package com.evan.webadmin.biz.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.util.MyPageUtil;
import com.evan.webadmin.biz.dto.EvluateSurveyDto;
import com.evan.webadmin.biz.dto.EvluateSurveyFilter;
import com.evan.webadmin.biz.model.EvluateSurvey;
import com.evan.webadmin.biz.model.EvluateSurveyDetail;
import com.evan.webadmin.biz.service.IEvluateSurveyDetailService;
import com.evan.webadmin.biz.service.IEvluateSurveyService;
import com.evan.webadmin.biz.vo.SubjectFrontVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评价问卷表 前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2025-03-04
 */
@RestController
@RequestMapping("/admin/biz/evluate/survey")
public class EvluateSurveyController {

    @Autowired
    IEvluateSurveyService surveyService;
    @Autowired
    IEvluateSurveyDetailService surveyDetailService;

    @PostMapping("/findByPage")
    public ResponseResult<MyPageData<EvluateSurvey>> findByPage(@MyRequestBody EvluateSurveyFilter surveyFilter,
                                                                     @MyRequestBody MyPageParam pageParam){
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<EvluateSurvey> list = surveyService.findByPage(surveyFilter);
        long totalCount = 0L;
        if (list instanceof Page) {
            totalCount = ((Page<EvluateSurvey>) list).getTotal();
        }
        return ResponseResult.success(MyPageUtil.makeResponseData(list, totalCount));
    }

    @PostMapping("/findById")
    public ResponseResult<EvluateSurvey> findByPage(@MyRequestBody Long id){
        return ResponseResult.success(surveyService.getById(id));
    }

    @PostMapping("/edit")
    public ResponseResult findByPage(@RequestBody EvluateSurveyDto surveyDto){
        surveyService.edit(surveyDto);
        return ResponseResult.success();
    }

    @PostMapping("/publish")
    public ResponseResult publish(@MyRequestBody Long id) {
        surveyService.publish(id);
        return ResponseResult.success();
    }

    @PostMapping("/detail")
    public ResponseResult<List<SubjectFrontVo>> detail(@MyRequestBody Long id) {
        List<EvluateSurveyDetail> details = surveyDetailService.detail(id);
        List<SubjectFrontVo> subjectFrontVos =  details.stream().map(i -> {
            return JSONUtil.toBean(i.getSubjectContent(), SubjectFrontVo.class);
        }).collect(Collectors.toList());
        return ResponseResult.success(subjectFrontVos);
    }

    @PostMapping("/publish/get")
    public ResponseResult<EvluateSurvey> publishGet() {
        return ResponseResult.success(surveyService.findPublishedPaper());
    }

    @PostMapping("/list")
    public ResponseResult<List<EvluateSurvey>> list(@MyRequestBody EvluateSurveyFilter filter) {
        QueryWrapper<EvluateSurvey> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return ResponseResult.success(surveyService.list(queryWrapper));
    }

    @PostMapping("/logic/delete")
    public ResponseResult logicDelete(@MyRequestBody Long surveyId) {
        surveyService.logicDelete(surveyId);
        return ResponseResult.success();
    }
}

